package com.ruoyi.erp.controller;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.domain.Payroll;
import com.ruoyi.erp.domain.SearchMonthBill;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.StaffingMonthBill;
import com.ruoyi.erp.service.IStaffingMonthBillService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 外派月账单Controller
 * 
 * @author sauger
 * @date 2022-03-17
 */
@Controller
@RequestMapping("/erp/bill")
public class StaffingMonthBillController extends BaseController
{
    private String prefix = "erp/bill";

    @Autowired
    private IStaffingMonthBillService staffingMonthBillService;

    @RequiresPermissions("erp:bill:view")
    @GetMapping()
    public String bill()
    {
        return prefix + "/bill";
    }

    /**
     * 查询外派月账单列表
     */
    @RequiresPermissions("erp:bill:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SearchMonthBill search)
    {
        startPage();
        List<StaffingMonthBill> list = staffingMonthBillService.searchStaffingMonthBill(search);
        return getDataTable(list);
    }

    @RequiresPermissions("erp:bill:form")
    @Log(title = "外派月账单", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean calculate, boolean update) throws Exception
    {
        ExcelUtil<StaffingMonthBill> util = new ExcelUtil<StaffingMonthBill>(StaffingMonthBill.class);
        List<StaffingMonthBill> list = util.importExcel(file.getInputStream());
        String operName = getLoginName();
        String message = staffingMonthBillService.importData(list, operName, calculate, update);
        return AjaxResult.success(message);
    }

    /**
     * 导出外派月账单列表
     */
    @RequiresPermissions("erp:bill:export")
    @Log(title = "外派月账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StaffingMonthBill staffingMonthBill)
    {
        List<StaffingMonthBill> list = staffingMonthBillService.selectStaffingMonthBillList(staffingMonthBill);
        ExcelUtil<StaffingMonthBill> util = new ExcelUtil<StaffingMonthBill>(StaffingMonthBill.class);
        return util.exportExcel(list, "外派月账单数据");
    }

    /**
     * 新增外派月账单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存外派月账单
     */
    @RequiresPermissions("erp:bill:add")
    @Log(title = "外派月账单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StaffingMonthBill staffingMonthBill)
    {
        return toAjax(staffingMonthBillService.insertStaffingMonthBill(staffingMonthBill));
    }

    /**
     * 修改外派月账单
     */
    @RequiresPermissions(value = {"erp:bill:edit", "erp:bill:form"}, logical = Logical.OR)
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        StaffingMonthBill staffingMonthBill = staffingMonthBillService.selectStaffingMonthBillById(id);
        mmap.put("staffingMonthBill", staffingMonthBill);
        return prefix + "/edit";
    }

    /**
     * 修改保存外派月账单
     */
    @RequiresPermissions(value = {"erp:bill:edit", "erp:bill:form"}, logical = Logical.OR)
    @Log(title = "外派月账单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StaffingMonthBill staffingMonthBill)
    {
        staffingMonthBill.setUpdateBy(getLoginName());
        return toAjax(staffingMonthBillService.updateStaffingMonthBill(staffingMonthBill));
    }

    /**
     * 删除外派月账单
     */
    @RequiresPermissions("erp:bill:remove")
    @Log(title = "外派月账单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(staffingMonthBillService.deleteStaffingMonthBillByIds(ids));
    }

    @RequiresPermissions("erp:bill:calculate")
    @PostMapping("/calculate")
    @ResponseBody
    public AjaxResult calculateById(Integer id)
    {
        staffingMonthBillService.calculateMonthBillById(id);
        return toAjax(true);
    }

    @RequiresPermissions(value = {"erp:bill:calculate","erp:bill:form"}, logical = Logical.OR)
    @GetMapping("/init")
    public String initView(){
        return prefix+ "/init";
    }

    @RequiresPermissions(value = {"erp:bill:calculate","erp:bill:form"}, logical = Logical.OR)
    @PostMapping("/init")
    @ResponseBody
    public AjaxResult initialMonthBill(StaffingMonthBill bill)
    {
        staffingMonthBillService.initMonthBills(bill.getBillYear(), bill.getBillMonth());
        return toAjax(true);
    }

    //计算指定账单的毛利
    @RequiresPermissions("erp:bill:calculate")
    @PostMapping("profit")
    @ResponseBody
    public AjaxResult calculateProfitRate(String ids)
    {
        String[] billIds =  Convert.toStrArray(",", ids);
        List<StaffingMonthBill> list = staffingMonthBillService.selectStaffingMonthBillByIds(billIds);
        list.forEach(bill->{
            staffingMonthBillService.calculateProfitRate(bill);
        });
        return toAjax(true);
    }

    @RequiresPermissions("erp:settle:add")
    @GetMapping("/settle/add")
    public String  viewSettleAdd()
    {
        return prefix+ "/settle/bill_list";
    }

    @RequiresPermissions("erp:settle:add")
    @GetMapping("/settle/add/{id}")
    public String  viewSettleAdd(@PathVariable("id") Integer id, ModelMap mmap)
    {
        mmap.put("customerId", id);
        return prefix+ "/settle/bill_list";
    }

    @RequiresPermissions("erp:settle:add")
    @PostMapping("/settle/list")
    @ResponseBody
    public TableDataInfo settleList(String ids)
    {
        String[] billIds =  Convert.toStrArray(",", ids);
        List<StaffingMonthBill> list = staffingMonthBillService.selectStaffingMonthBillByIds(billIds);
        return getDataTable(list);
    }

    @RequiresPermissions("erp:settle:add")
    @PostMapping("/settle/chose")
    @ResponseBody
    public TableDataInfo settleChose(StaffingMonthBill staffingMonthBill)
    {
        List<StaffingMonthBill> list = staffingMonthBillService.selectStaffingMonthBillList(staffingMonthBill);
        return getDataTable(list);
    }

}
